All files / Bandstand/src/app/api/songs/[id]/chart route.ts

94.11% Statements 32/34
83.33% Branches 5/6
100% Functions 0/0
94.11% Lines 32/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 381x   1x 1x 1x 1x   1x 1x 1x 1x 1x   1x 1x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 4x 1x 1x 1x 1x 1x 1x 1x     4x 1x  
export const dynamic = 'force-dynamic'
 
import {z} from 'zod'
import {requireSession} from '@/lib/guard'
import {ConflictError, saveChart} from '@/lib/songs'
import {route} from '@/lib/route'
 
const Body = z.object({
  source: z.string().min(1).max(100_000),
  note: z.string().trim().max(500).nullable().optional(),
  baseNumber: z.number().int().min(0),
})
 
export const POST = route(
  async (req: Request, {params}: {params: {id: string}}) => {
    const {user, band} = await requireSession()
    const parsed = Body.safeParse(await req.json())
    if (!parsed.success) return new Response('Bad Request', {status: 400})
    try {
      const v = await saveChart({
        bandId: band.id,
        songId: params.id,
        userId: user.id,
        ...parsed.data,
      })
      if (!v) return new Response('Not Found', {status: 404})
      return Response.json({number: v.number}, {status: 201})
    } catch (e) {
      if (e instanceof ConflictError)
        return Response.json(
          {error: e.message, latest: e.latest},
          {status: 409},
        )
      throw e
    }
  },
)